package com.jhy.review.leetcode;

import java.util.HashMap;
public class code05_RomanToInt {
    public static int romanToInt(String s) {
        int res = 0;
        HashMap<Character, Integer> hashMap = new HashMap<>();
        hashMap.put('I',1);
        hashMap.put('V',5);
        hashMap.put('X',10);
        hashMap.put('L',50);
        hashMap.put('C',100);
        hashMap.put('D',500);
        hashMap.put('M',1000);
        int length = s.length();
        // 定义一个初始值
        int prenum = hashMap.get(s.charAt(0));
        for (int i = 1; i < length; i++) {
            // 当前值
            int curnum = hashMap.get(s.charAt(i));
            if(prenum < curnum){
                res -= prenum;
            }else {
                res += prenum;
            }
            prenum = curnum;
        }
        // 加上最后一个值 最后一个数右边没有数了
        res += prenum;
        return res;
    }
    public static void main(String[] args) {
        int i = romanToInt("III");
        System.out.println(i);
    }
}
